using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

using CE.User;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive") == "1")
        {
            //Response.Redirect("~/");
        }
        

        if (!Page.IsPostBack)
        {
            //CheckLink();
            string[] languages = HttpContext.Current.Request.UserLanguages;
            if (languages != null && languages.Length != 0)
            {
                string _logSting = "Security page first time loaded. Language = ";
                for (int i = 0; i < languages.Length; i++)
                {
                    _logSting +=  "[" + languages[i].ToString() + "] ";
                }
                LogUserViewedHistory(_logSting);
                //Response.Write(languages[0].ToString());
            }
            else
            {
                LogUserViewedHistory("Security page first time loaded.");
            }
            /* for admin test only*/
            //TextBox1.Text = "ceadmin";
            //TextBox2.Attributes["value"] = "@XpressWay";
            //Session[CE.Global.General.Guest] = "Admin"; ;
            //LogUserViewedHistory("Security page [admin=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
            //Response.Redirect("~/Home.aspx");
            /* Test */

            /* for Provider test only*/
            //TextBox1.Text = "liberty";
            //TextBox2.Attributes["value"] = "lp@286";
            //Session[CE.Global.General.Guest] = "Provider";
            //LogUserViewedHistory("Security page [provider=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
            //Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
    }

    protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AdminID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AdminPass"))
                {
                    Session[CE.Global.General.Guest] = "Admin";
                    LogUserViewedHistory("Security page [admin=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    //Response.Redirect("~/home.aspx");
                    Response.Redirect("~/");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.UserID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.UserPass"))
                {
                    Session[CE.Global.General.Guest] = "User";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    //Response.Redirect("~/home.aspx");
                    Response.Redirect("~/");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ProviderTestID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ProviderTestPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ProviderTestCompanyID");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AZCPID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AZCPPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AZCPProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else if (TextBox1.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIID"))
            {
                if (TextBox2.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIPass"))
                {
                    Session[CE.Global.General.Guest] = "Provider";
                    Session[CE.Global.General.UserTypeId] = CE.Global.CEUserType.Provider.ToString();
                    Session[CE.Global.General.ProviderCompanyId] = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIProviderCompanyId");
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login passed.");
                    Response.Redirect("~/provider/providersecurelogin.aspx");
                }
                else
                {
                    lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
                    LogUserViewedHistory("Security page [user=" + TextBox1.Text + ";password=" + TextBox2.Text + "] login failed.");
                }
            }
            else
            {
                lblMessage.Text = "Ooops, please try again.<br/>You keyed something wrong.";
            }
        }
        else
        {
            
            LogUserViewedHistory("Security page [userid=" + TextBox1.Text + ";password=" + TextBox2.Text + "] invalid login.");
        }

    }

    private void LogUserViewedHistory(string Description)
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("CX", Request, Description);
    }

    private void CheckLink()
    {
        lblCheckStatus.Text = GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AllianceWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIWorkshopTH"));
        lblCheckStatus.Text = lblCheckStatus.Text + GetData(CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MTIWorkshopTH"));

    }
    private string GetData(string URL)
    {
        string anstr = "";
        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
            req.Method = "GET";
            req.Referer = URL;
            HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse();
            //Stream stream = webResponse.GetResponseStream();
            //StreamReader answer = new StreamReader(stream);
            //anstr = answer.ReadToEnd();
            anstr = webResponse.StatusCode.ToString();
        }
        catch (WebException Wex)
        {
            anstr = "Error";
        }
        catch (Exception Ex)
        {
            anstr = "Error";
        }
        return anstr;
    }
}